package com.yuexiao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuexiao.config.RedisUtil;
import com.yuexiao.pojo.ShoppingCart;
import com.yuexiao.service.DishService;
import com.yuexiao.service.ShoppingCartService;
import com.yuexiao.mapper.ShoppingCartMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
 * @createDate 2022-08-10 14:10:57
 */
@Component
@Service(interfaceClass = ShoppingCartService.class)
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
        implements ShoppingCartService {

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 根据用户id查询购物车列表
     *
     * @param userid Long 用户id
     * @return List<ShoppingCart> 购物车列表
     */
    @Override
    public List<ShoppingCart> getShoppingCartListByUserId(Long userid) {
        return baseMapper.selectList(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, userid));
    }


    /**
     * 新增购物车记录
     *
     * @param shoppingCart ShoppingCart 购物车数据对象
     * @return ShoppingCart 新增数据对象
     */
    @Override
    public ShoppingCart insertShopping(ShoppingCart shoppingCart) {

        //从redis中获取登录用户id
        Object loginId = redisUtil.get("loginId");
        if (loginId == null) {
            throw new RuntimeException("未登录");
        }
        shoppingCart.setUserId(Long.valueOf(String.valueOf(loginId)));


        //设置查询条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, loginId);

        //判断添加的是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //查询当前菜品或套餐是否在购物车
        ShoppingCart cartServieOne = baseMapper.selectOne(queryWrapper);
        //判断如果存在就在原来的基础上加1，如果不存在就添加到购物车，默认值为1
        if (cartServieOne != null) {
            Integer number = cartServieOne.getNumber();
            cartServieOne.setNumber(number + 1);
            baseMapper.updateById(cartServieOne);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(new Date());
            baseMapper.insert(shoppingCart);
            cartServieOne = shoppingCart;
        }

        return cartServieOne;
    }


    /**
     * 根据套餐id或者菜品id更改商品数量
     *
     * @param shoppingCart ShoppingCart 购物车数据对象
     * @return ShoppingCart 购物车数据对象
     */
    @Override
    public ShoppingCart updateShoppingCartByDishIdOrSetmealId(ShoppingCart shoppingCart) {

        //从redis中获取登录用户id
        Object loginId = redisUtil.get("loginId");
        if (loginId == null) {
            throw new RuntimeException("未登录");
        }
        shoppingCart.setUserId(Long.valueOf(String.valueOf(loginId)));

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, loginId);

        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart cart = baseMapper.selectOne(queryWrapper);

        //判断商品数量，如果大于1就减去1，如果等于1则直接删
        if (cart.getNumber() > 1) {
            shoppingCart.setNumber(cart.getNumber() - 1);
            shoppingCart.setId(cart.getId());
            baseMapper.updateById(shoppingCart);
        } else {
            shoppingCart.setNumber(cart.getNumber() - 1);
            shoppingCart.setId(cart.getId());
            baseMapper.deleteById(cart.getId());
        }
        return shoppingCart;
    }

    /**
     * 根据用户id删除购物车数据
     * @param id Long 用户id
     * @return boolean true or false
     */
    @Override
    public boolean deleteShoppingCartByUserId(Long id) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,id);
        baseMapper.delete(queryWrapper);
        return true;
    }
}




